<template>
	<view>
		<u-sticky>
			<u-tabs :list="list" :is-scroll="false" :current="current" @change="sectionChange"></u-tabs>
		</u-sticky>

		<view v-if="current == 1" class=" u-p-r-30 u-p-l-30 u-m-t-20">
			<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266"
				@custom="search" @search="search"></u-search>
		</view>
		
		<u-card v-if="current == 0">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>取货点</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>目的地</span>
							</view>
						</u-col>
						<u-col span="4" text-align="center">
							<view class=" weikaishi">
								<span>创建人</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in dataList" :key="index" @click="receive(item)">
					<u-col span="4" text-align="center">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{item.srcPositionCode}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="center">
						<view class="u-m-t-10 huanhang" >
							<span>{{item.tarPositionCode}}</span>
						</view>
					</u-col>
					<u-col span="4" text-align="center">
						<view class="u-m-t-10 huanhang" >
							<span>{{item.createUserName}}</span>
						</view>
					</u-col>
					<u-col span="12" text-align="center">
						<view class="jiange">
						</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		
		<u-card v-if="current == 1">
			<view class="" slot="head">
				<view class="wrap">
					<u-row>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>创建人</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>创建时间</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>目的地</span>
							</view>
						</u-col>
						<u-col span="3" text-align="center">
							<view class=" weikaishi">
								<span>领取人</span>
							</view>
						</u-col>
					</u-row>
				</view>
			</view>
			<view class="" slot="body">
				<u-row v-for="(item,index) in dataList2" :key="index" @click="toput(item.id,item.transportUser)">
					<u-col span="3" text-align="center">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{item.createUserName}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="center">
						<view class="lanse u-m-t-10 huanhang">
							<span>{{item.createTime}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.tarPositionCode}}</span>
						</view>
					</u-col>
					<u-col span="3" text-align="center">
						<view class="u-m-t-10 huanhang">
							<span>{{item.transportUserName}}</span>
						</view>
					</u-col>
					<u-col span="12" text-align="center">
						<view class="jiange">
						</view>
					</u-col>
				</u-row>
			</view>
		</u-card>
		<view style="height: 90rpx;" v-if="current == 0">
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				flex: '',
				keyword: '',
				list: [{
						name: '待领取'
					},
					{
						name: '已领取'
					}
				],
				arealist: [],
				current: 0,
				dataList: [],
				dataList2: [],
				pageNo: 1, //第几页
				hasMore: true, //是否有下一页
				orderCount: 0, //总共多少条
			}
		},
		onLoad() {
			//#ifdef H5 || APP-PLUS
			this.flex = 'u-flex'
			//#endif

			this.$http.post('/rest/storage/Arealist', {}, {
				params: {},
				header: {
					'Authorization': uni.getStorageSync('token'),
				}
			}).then(res => {
				this.areaName = res.data.data[0].areaName
				for (let item1 of res.data.data) {
					let obj = {}
					obj.name = item1.areaName
					obj.orderCnt = item1.orderCnt
					this.arealist.push(obj)
				}
				console.log(this.arealist)
			})
		},
		onShow() {
			this.refresh()
		},
		onPullDownRefresh() {
			this.refresh()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.current == 0) {
				uni.showLoading({
					title: '加载中'
				})
				if (this.hasMore) {
					this.pageNo++
					setTimeout(() => {
						this.loadOrder({
							condition: this.keyword
						}).then(res => {})
						uni.hideLoading()
					}, 500)
				} else {
					setTimeout(() => {
						uni.hideLoading()
						uni.showToast({
							icon: 'none',
							title: '已经到低了哦',
							duration: 2000
						});
					}, 500)
				}
			}
		},
		methods: {
			toput(id, transportUser) {
				this.$http.post('/rest/hxStorageOffShelf/findUser2', {}, {
					params: {
						account: transportUser
					},
					header: {
						'Authorization': uni.getStorageSync('token'),
					}
				}).then(res => {
					if (res.data.code == 200) {
						this.$Router.push({
							name: 'xh_goods_transport_detailed',
							params: {
								id: id
							},
						})
					} else {
						this.$refs.uToast.show({
							title: res.data.message,
							type: 'error'
						})
					}
				})
			},

			receive(item){
				uni.showModal({
					title:'确认领取吗？',
					success: (res) => {
						if (res.confirm) {
							this.$http.post('/rest/xhTransportController/receive',{
							},{
								params: {
									id: item.id,
								},
								header: {
									'Authorization' :uni.getStorageSync('token'),
								}
							}).then(res => {
								if(res.data.code == 200) {
									this.$refs.uToast.show({
										title: '领取成功',
										type: 'success'
									})
									this.$Router.push({
										name: 'xh_goods_transport_detailed',
										params: { id:item.id},
									})
								} else {
									this.$refs.uToast.show({
										title: res.data.message,
										type: 'error'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				})
			},
			
			refresh() {
				if (this.current == 0) {
					this.pageNo = 1
					this.hasMore = true
					this.loadOrder({
					}).then(res => {})
				} else if (this.current == 1) {
					this.$http.post('/rest/xhTransportController/queryAllItem', {}, {
						params: {
							condition: this.keyword
						},
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						this.dataList2 = res.data.data
						this.list[1].name = '已领取(' + res.data.data.length + ')'
					})
				}
			},
			loadOrder(param = {}) {
				return new Promise((resolve, reject) => {
					param = Object.assign(param, {
						page: this.pageNo,
						limit: 20
					})
					this.$http.post('/rest/xhTransportController/queryItem', {}, {
						params: param, 
						header: {
							'Authorization': uni.getStorageSync('token'),
						}
					}).then(res => {
						if (this.pageNo == 1) {
							this.dataList = res.data.data
						} else {
							this.dataList = this.dataList.concat(res.data.data)
						}
						this.orderCount = res.data.count
						this.list[0].name = '待领取('+res.data.count+')'
						this.hasMore = this.pageNo * 20 < res.data.count
						resolve(res)
					})
				})
			},
			sectionChange(e) {
				this.current = e
				uni.showLoading({
					title: '加载中'
				})
				this.refresh()
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			},
			search(e) {
				uni.showLoading({
					title: '加载中'
				})
				this.refresh()
				setTimeout(() => {
					uni.hideLoading()
				}, 500)
			},
			radioChange(e) {
				console.log(e);
			},
		}
	}
</script>


<style lang="scss">
	.u-body-item {
		padding: 20rpx 15rpx;
	}

	.wrap {
		padding: 0rpx 12rpx;
	}

	.u-row {
		margin: 10rpx 2rpx;
	}

	.weikaishi2 {
		font-size: 34rpx;
	}

	.weikaishi3 {
		font-size: 30rpx;
	}

	.jiange {
		border-bottom: 1px solid #ebebeb;
	}

	.huanhang {
		word-break: break-all;
	}

	.baise {
		background-color: #2979ff;
		color: #ffffff;
		font-size: 45rpx;
	}
</style>
